using System;
using System.Linq;
using Granite.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Xml.Linq;
using Granite.Modeling;

namespace Toon.Models.Characters
{
	public class AttributeCollection : ChangeTrackingModelCollection<Attribute> //TODO: Make this a read-only collection
	{
		public AttributeCollection()
		{
		}

		public AttributeCollection(ObservableCollection<Attribute> list)
			: base(list)
		{

		}

		public AttributeCollection(IEnumerable<Attribute> source)
			: base(source)
		{

		}

		public Attribute this[string name]
		{
			get { return this.Single(a => a.Name == name); }
		}

		internal IList<XElement> ToXml()
		{
			return (from a in this select a.ToXml()).ToList();
		}

		internal void FromXml(XElement source)
		{
			foreach (var node in source.Elements())
			{
				var attribute = this[node.Attribute("Name").Value];
				attribute.FromXml(node);
			}
		}
	}
}
